import pymysql
from core import settings
from lib import conf_common


class UseMysql:
    """获取配置文件信息"""
    base = conf_common.UseCofg(settings.Cfg_path)

    def __init__(self):
        self.con = pymysql.connect(host=self.base.use_cofg("mysql", "host"),
                                   port=int(self.base.use_cofg("mysql", "port")),
                                   user=self.base.use_cofg("mysql", "user"),
                                   password=self.base.use_cofg("mysql", "password"),
                                   db=self.base.use_cofg("mysql", "db"))

    """查看数据"""

    def select_one(self, sql):
        cur = self.con.cursor()  # 创建游标对象
        cur.execute(sql)
        res = cur.fetchone()
        self.con.commit()
        cur.close()
        return res

    """查看个数"""

    def select_count(self, sql):
        cur = self.con.cursor()  # 创建游标对象
        res = cur.execute(sql)
        self.con.commit()
        cur.close()
        return res

    def __del__(self):
        if self.con:
            self.con.close()
